﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using PessModel;
//using PessMvc.Models;
namespace PessMvc.Lib
{
    public class ExpertJudgeBL:IExpertJudgeBL
    {
        private PESSEntities db;

        public ExpertJudgeBL()
        {
            db = new PESSEntities();
        }

        public bool Add(ExpertJudge expert, List<ExpertGroup> expertGroups,out string errorMsg, string password = "111111")
        {
            if (expertGroups == null || expertGroups.Count == 0)
            {
                errorMsg = "没有选择评委组！请选择所属评委组。";
                return false;
            }
            MembershipCreateStatus createStatus;
            expert.Email=expert.Email == null ? expert.SerialNumber + "@b.com" : expert.Email;
            MembershipUser user = Membership.CreateUser(expert.SerialNumber, password, expert.Email, "question", "answer", true, null, out createStatus);
            if (createStatus == MembershipCreateStatus.Success)
            {
                Roles.AddUserToRole(user.UserName, "ExpertJudge");
                expert.UserID = new Guid(user.ProviderUserKey.ToString());

                //db.ExpertGroups.Find().ExpertJudges.Add

                expert.ExpertGroups = null;
                db.ExpertJudges.Add(expert);
                db.SaveChanges();
                
                foreach (var group in expertGroups)
                {
                    var g = db.ExpertGroups.Find(group.ID);
                    g.ExpertJudges.Add(expert);
                }
                db.SaveChanges();

                user.Email = expert.Email;
                Membership.UpdateUser(user);   
                errorMsg = null;

                //expert = db.ExpertJudges.Find(expert.SerialNumber);
                

                return true;// RedirectToAction("Details", "ExpertGroup", new { id = expert.ExpertGroupID });
            }
            else
            {
                errorMsg = createStatus.ToString();
                return false;
                //ModelState.AddModelError("", createStatus.ToString());
            }
        }

        public bool IfExpertExists(int? expertGroupID, string realName)
        {
            bool ifExists = false;
            int count = db.ExpertJudges.Where(e => e.ExpertGroupID == expertGroupID && e.RealName.Trim() == realName.Trim()).Count();
            if (count >= 1)//Expert with the same realname already exist in the same expert group.
            {
                ifExists = true;
            }
            return ifExists;
        }

        public bool IfExpertGroupMatchesActivity(int activityID, int? expertGroupID)
        {
            bool flagValue = false;
            var expertGroup = db.ExpertGroups.Find(expertGroupID);
            if (expertGroup != null && expertGroup.Subject.ActivityID == activityID)
            {
                flagValue = true;
            }
            return flagValue;
        }

        public ExpertJudge Find(string serialNumber)
        {
            var judge=db.ExpertJudges.Find(serialNumber);
            if (judge == null)
            {
                return judge;
            }

            var user = Membership.GetUser(serialNumber.Trim());
            judge.Email = user.Email;

            return judge;
        }
    }
}